<?php
require_once(dirname(__FILE__) . '/PortalUtil.class.php');

class Dog extends PortalUtil {
	public $tbl = Constant::tbl_wifi_connection;

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		$this->pretreat($controller);
		
		$url = $_SESSION['url'];

		$controller->display($request, 'wlan/index');
		
		// redirect('http://www.baidu.com', 'location', 302);
		
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function message($controller, $request) {
		$this->pretreat($controller);
		
		$message = $request["message"];
		
		if (isset($message)) {
			switch ($message) {
				case 'failed_validation':
					//auth的stage为login时，被服务器返回AUTH_VALIDATION_FAILED时，来到该处处理
					//认证失败，请重新认证
					break;
				case 'denied':
					//auth的stage为login时，被服务器返回AUTH_DENIED时，来到该处处理
					//认证被拒
		
					break;
				case 'activate':
					//auth的stage为login时，被服务器返回AUTH_VALIDATION时，来到该处处理
					//待激活
					break;
				default:
					break;
			}
		} else {
			//不回显任何信息
			echo '';
		}
		
		// $controller->display($request, 'wifi/message');
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function ping($controller, $request) {
		
		require_once(dirname(__FILE__) . '/Node.class.php');
		$aa = new Node();
		
		$aa->_update($request);
		
		echo 'Pong';
	}

	function auth($controller, $request) {
		debug(print_r($request, true));
		
		$token = $request['token'];
		$user = $this->_user_by_token($token);
		if (!$user) {
			echo "Auth: 0";
			return false;
		}
		
		$stage = $request['stage'];
		if ($stage == 'login') {
			$this->_login($request, $user);
			echo "Auth: 1";
		} else if ($stage == 'logout') {
			$this->_logout($request, $user);
			
			echo "Auth: 0";
		} else if ($stage == 'counters') {
			$request = $this->_update_user($request, $user);
			
			$this->_counters($request, $user);
			
			if ($this->_valid($request, $user)) {
				echo "Auth: 1";
			} else {
				echo "Auth: 0";
			}	
		} else {
			echo "Auth: 0";
		}
	}
	
	function _login($request, $user) {
		require_once(dirname(__FILE__) . '/Log.class.php');
		$aa = new Log();
		
		$aa->_login($request, $user);
	}
	
	function _logout($request, $user) {
		require_once(dirname(__FILE__) . '/Log.class.php');
		$aa = new Log();
		
		$aa->_logout($request, $user);	
	}
	
	function _counters($request, $user) {
		$office = $user['office'];
		$dept = $user['dept'];
		$emp = $user['emp'];
		
		$uid = $user['id'];
		
		$token = $request['token'];
		$gw_id = $request['gw_id'];
		
		$ip = $request['ip'];
		$mac = $request['mac'];
		
		$incoming = $request['incoming'];
		$outgoing = $request['outgoing'];
		
		$bytes = $request['bytes'];
		$period = $request['period'];
		$speed = $bytes / $period;
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, uid, token, gw_id, mac, ip, incoming, outgoing, bytes, `period`, speed, createtime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', NOW())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $uid, $token, $gw_id, $mac, $ip, $incoming, $outgoing, $bytes, $period, $speed);
		debug($sql);
		$db->query($sql);
		
		require_once(dirname(__FILE__) . '/Log.class.php');
		$aa = new Log();
		
		$aa->_counters($request, $user);
	}
	
	function _update_user($request, $user) {
		
		$request['createtime'] = time();
		
		$token = $request['token'];
		$key = sprintf("counters_%s", $token);
		
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				$request['bytes'] = $request['incoming'] + $request['outgoing'] - $ret['incoming'] - $ret['outgoing'];
				$request['period'] = $request['createtime'] - $ret['createtime'];
				
			} else {
				$request['bytes'] = $request['incoming'] + $request['outgoing'];
			}
			
			$this->_remain($request, $user);
			
			$mc->set($key, $request, 0, _strtotime("2 day"));
		}
		
		return $request;
	}
	
	function _remain($request, $user) {
		
		require_once(dirname(__FILE__) . '/User.class.php');
		$aa = new User();
		
		$aa->_remain($request, $user);
	}

	function _valid($request, $user) {
		
		require_once(dirname(__FILE__) . '/User.class.php');
		$aa = new User();
	
		return $aa->_valid($request, $user);
	}
	
	function _user_by_token($token) {
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($token);
			if (is_array($ret)) {
				return $ret;
			}
		}
	
		return false;
	}
	
	function _tbl($t1) {
		if (empty($t1)) {
			return $this->tbl;
		}
	
		$dd = date("Y-m-d");
	
		if ($t1 == $dd) {
			return $this->tbl;
		}
	
		$dd = date("Ymd", strtotime($t1));
		return sprintf("%s_%s", $this->tbl, $dd);
	}
}
?>
